/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.william;

import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.william.TeleportScroll;

// Referenced classes of package l1j.server.server.model:
// L1PcInstance

public class L1WilliamTeleportScroll {

	public static int checkItemId(final int itemId) {
		final L1WilliamTeleportScroll teleport_scroll = TeleportScroll
				.getInstance().getTemplate(itemId);

		if (teleport_scroll == null) {
			return 0;
		}

		final int item_id = teleport_scroll.getItemId();
		return item_id;
	}

	public static void getTeleportScroll(final L1PcInstance pc,
			final L1ItemInstance l1iteminstance, final int itemId) {
		final L1WilliamTeleportScroll teleport_scroll = TeleportScroll
				.getInstance().getTemplate(itemId);

		if (teleport_scroll == null) {
			return;
		}

		if (teleport_scroll.getCheckMinLocX() != 0
				&& teleport_scroll.getCheckMinLocY() != 0
				&& teleport_scroll.getCheckMaxLocX() != 0
				&& teleport_scroll.getCheckMaxLocY() != 0
				&& pc.getX() >= teleport_scroll.getCheckMinLocX()
				&& pc.getX() <= teleport_scroll.getCheckMaxLocX()
				&& pc.getY() >= teleport_scroll.getCheckMinLocY()
				&& pc.getY() <= teleport_scroll.getCheckMaxLocY()
				&& pc.getMapId() == teleport_scroll.getCheckMapId()) {
			if (pc.getMap().isEscapable() || pc.isGm()) {
				L1Teleport.teleport(pc, teleport_scroll.getTpLocX(),
						teleport_scroll.getTpLocY(),
						(short) teleport_scroll.getTpMapId(), 5, true);
				if (teleport_scroll.getRemoveItem() != 0) {
					pc.getInventory().removeItem(l1iteminstance, 1);
				}
			} else {
				pc.sendPackets(new S_ServerMessage(647));
			}
		} else if (teleport_scroll.getCheckMinLocX() == 0
				&& teleport_scroll.getCheckMinLocY() == 0
				&& teleport_scroll.getCheckMaxLocX() == 0
				&& teleport_scroll.getCheckMaxLocY() == 0
				&& teleport_scroll.getTpLocX() != 0
				&& teleport_scroll.getTpLocY() != 0) {
			if (pc.getMap().isEscapable() || pc.isGm()) {
				L1Teleport.teleport(pc, teleport_scroll.getTpLocX(),
						teleport_scroll.getTpLocY(),
						(short) teleport_scroll.getTpMapId(), 5, true);
				if (teleport_scroll.getRemoveItem() != 0) {
					pc.getInventory().removeItem(l1iteminstance, 1);
				}
			} else {
				pc.sendPackets(new S_ServerMessage(647));
			}
		} else {
			pc.sendPackets(new S_ServerMessage(79));
		}
	}

	private final int _itemId;

	private final int _tpLocX;

	private final int _tpLocY;

	private final int _tpMapId;

	private final int _check_minLocX;

	private final int _check_minLocY;

	private final int _check_maxLocX;

	private final int _check_maxLocY;

	private final short _check_MapId;

	private final int _removeItem;

	public L1WilliamTeleportScroll(final int itemId, final int tpLocX,
			final int tpLocY, final short tpMapId, final int check_minLocX,
			final int check_minLocY, final int check_maxLocX,
			final int check_maxLocY, final short check_MapId,
			final int removeItem) {

		this._itemId = itemId;
		this._tpLocX = tpLocX;
		this._tpLocY = tpLocY;
		this._tpMapId = tpMapId;
		this._check_minLocX = check_minLocX;
		this._check_minLocY = check_minLocY;
		this._check_maxLocX = check_maxLocX;
		this._check_maxLocY = check_maxLocY;
		this._check_MapId = check_MapId;
		this._removeItem = removeItem;
	}

	public int getCheckMapId() {
		return this._check_MapId;
	}

	public int getCheckMaxLocX() {
		return this._check_maxLocX;
	}

	public int getCheckMaxLocY() {
		return this._check_maxLocY;
	}

	public int getCheckMinLocX() {
		return this._check_minLocX;
	}

	public int getCheckMinLocY() {
		return this._check_minLocY;
	}

	public int getItemId() {
		return this._itemId;
	}

	public int getRemoveItem() {
		return this._removeItem;
	}

	public int getTpLocX() {
		return this._tpLocX;
	}

	public int getTpLocY() {
		return this._tpLocY;
	}

	public int getTpMapId() {
		return this._tpMapId;
	}
}
